package zsj.androidknife.utilsImpl;


import android.support.annotation.NonNull;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;

import zsj.androidknife.ThreadPool.TaskExecutor;
import zsj.androidknife.utils.BackgroundAction;
import zsj.androidknife.utils.IFileUtils;
import zsj.androidknife.utils.UiAction;

public class FileUtils implements IFileUtils {

    private TaskExecutor executor;

    private static class InstanceHolder {
        public static FileUtils INSTANCE = new FileUtils();
    }

    public static FileUtils getInstance() {
        return InstanceHolder.INSTANCE;
    }

    private FileUtils() {
        this.executor = TaskExecutor.getInstance();
    }

    @Override
    public void readString(final String filePath, UiAction<String> uiAction) {
        File file = new File(filePath);
        if (!file.exists())
            return;
        readString(file, uiAction);
    }

    @Override
    public void readString(@NonNull File file, UiAction<String> uiAction) {
        executor.submitBackgroundForResult(new BackgroundAction<File, String>() {
            @Override
            public String doInBackground(File param) {
                InputStream is = null;
                BufferedReader reader = null;
                try {
                    StringBuilder builder = new StringBuilder();
                    is = new FileInputStream(param);
                    reader = new BufferedReader(new InputStreamReader(is));
                    String line = reader.readLine();
                    while (line != null) {
                        builder.append(line);
                        line = reader.readLine();
                        if (line != null)
                            builder.append("\n");
                    }
                    return builder.toString();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (reader != null) try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
        },uiAction,file);
    }

    @Override
    public void writeString(String string, String filePath) {
        writeString(string, filePath, null);
    }

    @Override
    public void writeString(String string, final File file, UiAction<Boolean> uiAction) {
        executor.submitBackgroundForResult(new BackgroundAction<String, Boolean>()
        {
            @Override
            public Boolean doInBackground(String stringParam)
            {
                PrintWriter out = null;
                try
                {
                    out = new PrintWriter(file);
                    out.print(stringParam);
                } catch (FileNotFoundException e)
                {
                    e.printStackTrace();
                    return false;
                } finally
                {
                    if (out != null)
                        out.close();
                }
                return true;
            }
        }, uiAction, string);
    }

    @Override
    public void writeString(String string, String filePath, UiAction<Boolean> uiAction) {
        File file = new File(filePath);
        boolean result = false;
        if (!file.exists()) {
            try {
                file.createNewFile();
                result = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else result = true;
        if (result) writeString(string, file, uiAction);
    }
}
